package com.quanjiaolian.DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;


import org.apache.log4j.Logger;



/**
 * 数据库连接Base类
 * @author Herson_Li
 * @date 2015-02-08 23:16:32
 */
public class DataBase {
	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(DataBase.class.getName());
	
	/**
	 * 声明数据库连接变量
	 */
	//定义一个Connection 用来连接数据库 
    private static Connection conn=null;  
    //连接数据库的URL  
    private static String url; 
    //数据库驱动
    private static String driver;
    //指定数据的用户名和密码  
    private static String username;  
    private static String password;  
    
    /**
     * 声明数据库连接实例
     */
    //声明PreparedStatement实例
    private static PreparedStatement pstmt = null;
    //用于存放返回结果的数据集
    private static ResultSet rs = null;
    /**
	 * 加载配置文件
	 * 
	 * @see Servlet#init(ServletConfig)
	 */

	public static void init() {
		// 读取配置文件
		try {
			// 读取文件
			resourceBundle = ResourceBundle.getBundle("jdbc");
			url = resourceBundle.getString("jdbc.url");
			driver = resourceBundle.getString("jdbc.driver");
			username = resourceBundle.getString("jdbc.username");
			password = resourceBundle.getString("jdbc.password");
			logger.info("*****************************init database connection info******************");
			logger.info("Loding jdbc properties success!");
		} catch (Exception e) {
			logger.error("Loading jdbc properties error!  " + e);
		}
	}
	/**
	 * 连接数据库
	 * @return Connection
	 */
	public static Connection getconn(){
		try {
			init();//从配置文件中获取连接信息
			logger.info("jdbc driver is: "+driver);
			logger.info("jdbc url is: "+url);
			logger.info("jdbc username is: "+username);
			logger.info("jdbc password is: "+password);
			Class.forName(driver);//加载数据库驱动文件
			conn = DriverManager.getConnection(url, username, password);//连接数据库
			logger.info("***************************************************************************");
		}catch (SQLException e2) {
			logger.error("Load jdbc driver faild!  "+e2);
			e2.printStackTrace();
		}catch (ClassNotFoundException e3) {
			logger.error("connection database faild!  "+e3);
			e3.printStackTrace();
		}
		return conn; 
	
	}
	/**
	 * 关闭所有数据库交互 释放数据库连接资源
	 */
	public void close(Connection conn,PreparedStatement pstmt,ResultSet rs){
		if(rs != null){
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				logger.error("close ResultSet faild!");
				e.printStackTrace();
			}
		}
		if(pstmt != null){
			try {
				pstmt.close();
				pstmt = null;
			} catch (SQLException e) {
				logger.error("close PreparedStatement faild!");
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
				logger.error("close Connection faild!");
				e.printStackTrace();
			}
		}
		logger.info("The database operation to complete, all successful connection is closed!");
	}
	/**
	 * 测试
	 * @param args
	 */
	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		DataBase db = new DataBase();
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement("select * from user_info");
			rs = pstmt.executeQuery();
			while(!rs.next()){
				System.out.println(rs.getInt(1));
				System.out.println(rs.getString(2));
				System.out.println(rs.getString(3));
				System.out.println(rs.getString(4));
				System.out.println(rs.getString(5));
			}
			db.close(conn, pstmt, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
